package com.vend.common.base

import android.annotation.SuppressLint
import android.content.Intent
import android.graphics.Bitmap
import android.os.Bundle
import android.util.Log
import android.view.KeyEvent
import android.view.ViewGroup
import android.webkit.WebResourceRequest
import android.webkit.WebView
import com.just.agentweb.AgentWeb
import com.just.agentweb.DefaultWebClient
import com.just.agentweb.WebChromeClient
import com.just.agentweb.WebViewClient
import com.vend.common.R
import com.vend.common.mvp.IPresenter

/**
 * Created by glendal on 2022/01/14.
 * 创建以AgentWeb第三方库的BaseWebActivity
 * 参考https://github.com/Justson/AgentWeb
 */
abstract class BaseWebActivity<T : IPresenter> : BaseActivity<T>() {
    protected var mAgentWeb: AgentWeb? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        Log.e("tag", "加载webView")
        initWebView()
    }

    /**
     * 初始化WebView
     */
    private fun initWebView() {

        mAgentWeb = AgentWeb.with(this)
            .setAgentWebParent(getWebViewContentView(), ViewGroup.LayoutParams(-1, -1))
            .useDefaultIndicator()
            .setWebChromeClient(mWebChromeClient)
            .setWebViewClient(mWebViewClient)
            .setMainFrameErrorView(R.layout.agentweb_error_page, R.id.tv_again)
            .setSecurityType(AgentWeb.SecurityType.STRICT_CHECK)
            .setWebLayout(WebLayout(this))
            .setOpenOtherPageWays(DefaultWebClient.OpenOtherPageWays.ASK) //打开其他应用时，弹窗咨询用户是否前往其他应用
            .interceptUnkownUrl() //拦截找不到相关页面的Scheme
            .createAgentWeb()
            .ready()
            .go(getUrl())
    }

    @SuppressLint("WrongViewCast")
    open fun getWebViewContentView(): ViewGroup {
        return findViewById(R.id.container)
    }

    /**
     * 获取网页
     */
    abstract fun getUrl() : String

    /**
     * 拦截网页的Url
     */
    abstract fun isLoadingDetailUrl(view : WebView?, request: WebResourceRequest?):Boolean

    /**
     * 拦截标题
     */
    abstract fun loadTitle(strTitle : String?)
    private val mWebViewClient = object : WebViewClient() {
        override fun shouldOverrideUrlLoading(
            view: WebView?,
            request: WebResourceRequest?
        ): Boolean {
            return if (isLoadingDetailUrl(view, request)){
                true
            }else {
                super.shouldOverrideUrlLoading(view, request)
            }
        }

        override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
            super.onPageStarted(view, url, favicon)
        }
    }
    private val mWebChromeClient = object : WebChromeClient() {
        override fun onReceivedTitle(view: WebView?, title: String?) {
            loadTitle(title)
            super.onReceivedTitle(view, title)
        }

        override fun onReceivedTouchIconUrl(view: WebView?, url: String?, precomposed: Boolean) {
            super.onReceivedTouchIconUrl(view, url, precomposed)
        }
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        return if (mAgentWeb?.handleKeyEvent(keyCode, event) == true) {
            true
        } else super.onKeyDown(keyCode, event)
    }

    override fun onPause() {
        mAgentWeb?.webLifeCycle?.onPause()
        super.onPause()
    }

    override fun onResume() {
        mAgentWeb?.webLifeCycle?.onResume()
        super.onResume()
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        Log.i("Info", "onResult:$requestCode onResult:$resultCode")
        super.onActivityResult(requestCode, resultCode, data)
    }

    override fun onDestroy() {
        super.onDestroy()
        mAgentWeb?.webLifeCycle?.onDestroy()
    }
}